package com.starbaba.stat.view;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.stat.model.StatActivityChannelModel;
import com.starbaba.stat.model.StatActivityModel;
import com.starbaba.stat.service.StatActivityChannelService;

@SuppressWarnings({"rawtypes", "unchecked", "serial"})
public class StatActivityChannelAction extends BaseAction implements ModelDriven<StatActivityChannelModel>{

	private StatActivityChannelModel model = null;
	
	private StatActivityChannelService service;
	
	private InputStream input;
	
	private String fileName;
	
	public String getFileName() {
		return fileName;
	}

	public InputStream getInput() {
		return input;
	}
	
	@Autowired
	@Qualifier("statActivityChannelService")
	public void setService(StatActivityChannelService service) {
		this.service = service;
	}

	public String showPage() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		try {
			service.showPage(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ActionSupport.SUCCESS;
	}

	public void queryList() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();

		try {
			List<StatActivityChannelModel> list = new ArrayList<StatActivityChannelModel>();
			list = service.getList(start, length, pageResponse, paramsMap);

			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());

			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String exportCVS() throws Exception {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		String value = service.getExportString(service.getList(null, null, pageResponse, paramsMap));
		input = new ByteArrayInputStream(value.getBytes("GBK"));
		fileName = System.currentTimeMillis() + ".csv";
		return ActionSupport.SUCCESS;
	}
	

	@Override
	public StatActivityChannelModel getModel() {
		if (model == null) {
			model = new StatActivityChannelModel();
		}
		return model;
	}

}
